function drawCanvas(){
						
	var canvasArray = document.getElementsByTagName("canvas");			
	var canvas_id;
	for (var i=0; i < canvasArray.length; i++){ 
 		canvas_id = canvasArray[i].getAttribute('id');
 		drawBar(canvas_id); 
	}
}
			
function drawBar(canvas_id){
	var c = document.getElementById(canvas_id);
			
	if (c != null) {
		//compute percent
		//format of canvas id: canvas_start_target_currentValue
		
		var canvasIdsplitted = canvas_id.split("_");
		
		if (canvasIdsplitted[2] == "yes" || canvasIdsplitted[2] == "no"){
			var start = canvasIdsplitted[2];
			var target = canvasIdsplitted[3];
			var value = canvasIdsplitted[4];
		}else{
			var start = parseFloat(canvasIdsplitted[2]);
			var target = parseFloat(canvasIdsplitted[3]);
			var value = parseFloat(canvasIdsplitted[4]);
		}
		
		
		var percentage = computePercentage(start,target,value);			
		var percentageInt = parseInt(percentage);
		
		// percentage column
		if (c.parentElement.nextSibling){
			var percentLabel = c.parentElement.nextSibling;
			percentLabel.innerHTML = percentageInt;
		}
		

		var ctx=c.getContext("2d");
		ctx.fillStyle="#00FF12";
		ctx.fillRect(0, 0, percentage, 10);
		ctx.fillStyle="#FF0000";
		ctx.fillRect(percentage, 0, 100, 10);
		ctx.strokeRect(0,0,100,10)
	
		ctx.moveTo(0,0);
		ctx.lineTo(100,0);
		ctx.lineTo(100,10);
		ctx.lineTo(0,10);
		ctx.lineTo(0,0);
		ctx.stroke();
	}
}
			
function computePercentage(start, target, current){
	
	if(start == "no" || start == "yes"){
		if (current == "yes"){
			return 100;
		}else{
			return 0;
		}
	}
	return Math.abs(current - start) * 100 / Math.abs(target - start);;
}

function addsubgoal(){

	var nextSubgoalId = 0;
	var subgoalstbody= document.getElementById("subgoalstbody");	
	
	
	if((document.getElementById("trsubgoal_0")) == null){
		// first subgoal
		nextSubgoalId = 0
	}else{
		// there is at least one subgoal
		var lastChildOfTbody = subgoalstbody.lastChild;
		while (true){			
			var lastChildOfTbody = subgoalstbody.lastChild;
			if (lastChildOfTbody != "[object HTMLTableRowElement]"){
				subgoalstbody.removeChild(lastChildOfTbody)
				continue;
			}
			var idOfLastSubgoal = lastChildOfTbody.getAttribute('id');
			var idsplitted = idOfLastSubgoal.split("_");
			nextSubgoalId = parseInt(idsplitted[1]) + 1;
			break;
		}
	}
	
	var newLine = 
	 		"<tr id=\"trsubgoal_"+nextSubgoalId+"\">" +
				"<td><input name=\"subgoalname_"+nextSubgoalId+"\" type=\"text\" value=\"\"/></td>"+
				"<td><input id=\"subgoalmeasurename_"+nextSubgoalId+"\" name=\"subgoalmeasurename_"+nextSubgoalId+"\" type=\"text\" value=\"\"/></td>"+
				"<td>"+
					"<select id=\"select_"+nextSubgoalId+"\" name=\"subgoalmeasuretype_"+nextSubgoalId+"\" onchange=\"subgoalTypeChosen(this);\">"+
						"<option name=\"integer\" value=\"1\">integer</option>"+
						"<option name=\"float\" value=\"2\">float</option>"+
						"<option name=\"money\" value=\"3\">money</option>"+
						"<option name=\"yesno\" value=\"4\">yes/no</option>"+
						"<option name=\"percent\" value=\"5\">percent</option>"+
					"</select>"+
				"</td>"+
				"<td><input id=\"subgoalstart_"+nextSubgoalId+"\" name=\"subgoalstartvalue_"+nextSubgoalId+"\" type=\"text\" value=\"\"/></td>"+
				"<td><input id=\"subgoalcurrent_"+nextSubgoalId+"\" name=\"subgoalcurrentvalue_"+nextSubgoalId+"\" type=\"text\" value=\"\"/></td>"+
				"<td><input id=\"subgoaltarget_"+nextSubgoalId+"\" name=\"subgoaltargetvalue_"+nextSubgoalId+"\" type=\"text\" value=\"\"/></td>"+
				"<td>"+
					"<select name=\"subgoalpriority_"+nextSubgoalId+"\">"+
						"<option name=\"urgimp\" value=\"ui\">1 - urgent / important</option>"+
						"<option name=\"urgnimp\" value=\"uni\">2 - urgent / unimportant</option>"+
						"<option name=\"nurgimp\" value=\"nui\">3 - unurgent / important</option>"+
						"<option name=\"nurgnimp\" value=\"nuni\">4 - unurgent / unimportant</option>"+
					"</select>"+
				"</td>"+
				"<td><input name=\"subgoaldeadline_"+nextSubgoalId+"\" type=\"date\" value=\"\"/></td>"+
			"</tr>";
		
		
		
		$('#subgoalstbody').append(newLine);
			
			var measurenameTextField = document.getElementById("measurename");	
			var startValueTextField = document.getElementById("startvalue");
			var currentValueTextField = document.getElementById("currentvalue");
			var targetValueTextField = document.getElementById("targetvalue");
			var measureTypeCombo = document.getElementById("measuretype");
			
			
			measurenameTextField.setAttribute('readonly', 'readonly');
			measurenameTextField.value= "%";
			
			startValueTextField.setAttribute('readonly', 'readonly');
			startValueTextField.value = "0";
			
			currentValueTextField.setAttribute('readonly', 'readonly');
			currentValueTextField.value = "0";
			
			targetValueTextField.setAttribute('readonly', 'readonly');
			targetValueTextField.value = "100";
			
			measureTypeCombo.disabled=true;
			measureTypeCombo.value = "5";

}

function deletesubgoal(){

	var subgoalstbody= document.getElementById("subgoalstbody");
	
	var measurenameTextField = document.getElementById("measurename");	
	var startValueTextField = document.getElementById("startvalue");
	var currentValueTextField = document.getElementById("currentvalue");
	var targetValueTextField = document.getElementById("targetvalue");
	var measureTypeCombo = document.getElementById("measuretype");
	
	
	while(true){
		var lastChildOfTbody = subgoalstbody.lastChild;
		if (lastChildOfTbody == "[object Text]"){
			subgoalstbody.removeChild(lastChildOfTbody);
			continue;
		}
		subgoalstbody.removeChild(lastChildOfTbody);
		break;
	}
		
	if ((document.getElementById("trsubgoal_0")) == null){
		measurenameTextField.removeAttribute("readonly");
		startValueTextField.removeAttribute("readonly");
		currentValueTextField.removeAttribute("readonly");
		targetValueTextField.removeAttribute("readonly");
		measureTypeCombo.disabled=false;  
		
		measurenameTextField.value = "";
		startValueTextField.value = "";
		currentValueTextField.value = "";
		targetValueTextField.value = "";
		measureTypeCombo.value = "1";
			
	}else{
	
		if (!measurenameTextField.hasAttribute('readonly')){
			measurenameTextField.setAttribute('readonly', 'readonly');
			startValueTextField.setAttribute('readonly', 'readonly');
			currentValueTextField.setAttribute('readonly', 'readonly');
			targetValueTextField.setAttribute('readonly', 'readonly');
		}else{
			measurenameTextField.readonly="readonly";
			startValueTextField.readonly="readonly";
			currentValueTextField.readonly="readonly";
			targetValueTextField.readonly="readonly";		
		}
		measureTypeCombo.disabled=true;
	}
}

function addsubgoalOnEditPage(){
	var timestamp = Date.now();
	var newline = 
	"<tr id=\"trsubgoal_"+timestamp+"\" name=\"trsubgoal_"+timestamp+"\"> "+							
		"<td><input name=\"subgoalname_"+timestamp+"\" type=\"text\" value=\"\"/></td>"+
		"<td><input name=\"subgoaldeadline_"+timestamp+"\" type=\"date\" value=\"\"/></td>"+
		"<td>"+
			"<select name=\"subgoalpriority_"+timestamp+"\">"+
				"<option name=\"urgimp\" value=\"ui\" selected>1 - urgent / important</option>"+
				"<option name=\"urgnimp\" value=\"uni\" >2 - urgent / unimportant</option>"+
				"<option name=\"nurgimp\" value=\"nui\" >3 - unurgent / important</option>"+
				"<option name=\"nurgnimp\" value=\"nuni\" >4 - unurgent / unimportant</option>"+
			"</select>"+
		"</td>"+
		"<td><input id=\"subgoalstart_"+timestamp+"\" name=\"subgoalstart_"+timestamp+"\" type=\"text\" value=\"\"/></td>"+					
		"<td><input id=\"subgoalcurrent_"+timestamp+"\" name=\"subgoalcurrent_"+timestamp+"\" type=\"text\" value=\"\"/></td>"+
		"<td><input id=\"subgoaltarget_"+timestamp+"\" name=\"subgoaltarget_"+timestamp+"\" type=\"text\" value=\"\"/></td>"+
		"<td><input id=\"subgoalmeasurename_"+timestamp+"\" name=\"subgoalmeasurename_"+timestamp+"\"type=\"text\" value=\"\"/></td>"+
		"<td>"+
			"<select id=\"select_"+timestamp+"\" name=\"subgoalmeasuretype_"+timestamp+"\" onchange=\"subgoalTypeChosen(this);\">"+
				"<option name=\"integer\" value=\"1\" selected>integer</option>"+
				"<option name=\"float\" value=\"2\" >float</option>"+
				"<option name=\"money\" value=\"3\" >money</option>"+
				"<option name=\"yesno\" value=\"4\" >yes/no</option>"+
				"<option name=\"percent\" value=\"5\" >percent</option>"+
			"</select>"+
		"</td>"+
		"<td>"+
		"<input type=\"button\" value=\"delete\" onclick=\"deleteSubgoalOnEditPage("+timestamp+")\"/>"+
		"</td>"+
	"</tr>";


	$('#subgoalstbody').append(newline);
	
	var measurenameTextField = document.getElementById("measurename");	
			var startValueTextField = document.getElementById("startvalue");
			var currentValueTextField = document.getElementById("currentvalue");
			var targetValueTextField = document.getElementById("targetvalue");
			var measureTypeCombo = document.getElementById("measuretype");
			
			
			measurenameTextField.setAttribute('readonly', 'readonly');
			measurenameTextField.value= "%";
			
			startValueTextField.setAttribute('readonly', 'readonly');
			startValueTextField.value = "0";
			
			currentValueTextField.setAttribute('readonly', 'readonly');
			currentValueTextField.value = "0";
			
			targetValueTextField.setAttribute('readonly', 'readonly');
			targetValueTextField.value = "100";
			
			measureTypeCombo.disabled=true;
			measureTypeCombo.value = "5";
	
}
	
function deleteSubgoalOnEditPage(subgoalid){
	var tr_id = "trsubgoal_"+subgoalid;
	$('#'+tr_id).remove();
	
	var measurenameTextField = document.getElementById("measurename");	
	var startValueTextField = document.getElementById("startvalue");
	var currentValueTextField = document.getElementById("currentvalue");
	var targetValueTextField = document.getElementById("targetvalue");
	var measureTypeCombo = document.getElementById("measuretype");
			
	if ((document.getElementsByTagName("tr")).length == 1){
		measurenameTextField.removeAttribute("readonly");
		startValueTextField.removeAttribute("readonly");
		currentValueTextField.removeAttribute("readonly");
		targetValueTextField.removeAttribute("readonly");
		measureTypeCombo.disabled=false;  
		
		measurenameTextField.value = "";
		startValueTextField.value = "";
		currentValueTextField.value = "";
		targetValueTextField.value = "";
		measureTypeCombo.value = "1";
			
	}else{
	
		if (!measurenameTextField.hasAttribute('readonly')){
			measurenameTextField.setAttribute('readonly', 'readonly');
			startValueTextField.setAttribute('readonly', 'readonly');
			currentValueTextField.setAttribute('readonly', 'readonly');
			targetValueTextField.setAttribute('readonly', 'readonly');
		}else{
			measurenameTextField.readonly="readonly";
			startValueTextField.readonly="readonly";
			currentValueTextField.readonly="readonly";
			targetValueTextField.readonly="readonly";		
		}
		measureTypeCombo.disabled=true;
	}
	
}

function goalTypeChosen(combo){

	var startValueTextField = document.getElementById("startvalue");
	var currentValueTextField = document.getElementById("currentvalue");
	var targetValueTextField = document.getElementById("targetvalue");
	var measurenameTextField = document.getElementById("measurename");	
	
	if(combo.value == "0" || combo.value == "1" || combo.value == "2" || combo.value == "3"){
		measurenameTextField.removeAttribute("readonly");
		startValueTextField.removeAttribute("readonly");
		currentValueTextField.removeAttribute("readonly");
		targetValueTextField.removeAttribute("readonly");
		measurenameTextField.value = "";
		startValueTextField.value = "";
		currentValueTextField.value = "";
		targetValueTextField.value = "";	
	}else if (combo.value == "4"){
		// yes/no
		startValueTextField.value = "no";
		currentValueTextField.value = "no"
		targetValueTextField.value = "yes";
		
		if (!measurenameTextField.hasAttribute('readonly')){
			startValueTextField.setAttribute('readonly', 'readonly');
			targetValueTextField.setAttribute('readonly', 'readonly');
		}else{
			startValueTextField.readonly="readonly";
			targetValueTextField.readonly="readonly";		
		}
	}else if(combo.value == "5"){
		// percent
		startValueTextField.value = "0";
		currentValueTextField.value = "0"
		targetValueTextField.value = "100";
		measurenameTextField.value = "%";
		
		if (!measurenameTextField.hasAttribute('readonly')){
			measurenameTextField.setAttribute('readonly', 'readonly');
			startValueTextField.setAttribute('readonly', 'readonly');
			targetValueTextField.setAttribute('readonly', 'readonly');
		}else{
			measurenameTextField.readonly="readonly";
			startValueTextField.readonly="readonly";
			targetValueTextField.readonly="readonly";		
		}
	}
	
}

function subgoalTypeChosen(combo){
	id = (combo.id.split("_"))[1];

	
	var startValueTextField = document.getElementById("subgoalstart_"+id);
	var currentValueTextField = document.getElementById("subgoalcurrent_"+id);
	var targetValueTextField = document.getElementById("subgoaltarget_"+id);
	var measurenameTextField = document.getElementById("subgoalmeasurename_"+id);	
	
	if(combo.value == "0" || combo.value == "1" || combo.value == "2" || combo.value == "3"){
		measurenameTextField.removeAttribute("readonly");
		startValueTextField.removeAttribute("readonly");
		currentValueTextField.removeAttribute("readonly");
		targetValueTextField.removeAttribute("readonly");
		measurenameTextField.value = "";
		startValueTextField.value = "";
		currentValueTextField.value = "";
		targetValueTextField.value = "";
	}else if (combo.value == "4"){
		// yes/no
		startValueTextField.value = "no";
		currentValueTextField.value = "no"
		targetValueTextField.value = "yes";
		
		if (!measurenameTextField.hasAttribute('readonly')){
			startValueTextField.setAttribute('readonly', 'readonly');
			targetValueTextField.setAttribute('readonly', 'readonly');
		}else{
			startValueTextField.readonly="readonly";
			targetValueTextField.readonly="readonly";		
		}
	}else if(combo.value == "5"){
		// percent
		startValueTextField.value = "0";
		currentValueTextField.value = "0"
		targetValueTextField.value = "100";
		measurenameTextField.value = "%";
		
		if (!measurenameTextField.hasAttribute('readonly')){
			measurenameTextField.setAttribute('readonly', 'readonly');
			startValueTextField.setAttribute('readonly', 'readonly');
			targetValueTextField.setAttribute('readonly', 'readonly');
		}else{
			measurenameTextField.readonly="readonly";
			startValueTextField.readonly="readonly";
			targetValueTextField.readonly="readonly";		
		}
	}
}

function preventChangesOfGoalValues(){
	if((document.getElementsByTagName('tr')).length > 1){
		
		document.getElementById('startvalue').setAttribute('readonly', 'readonly');
		document.getElementById('currentvalue').setAttribute('readonly', 'readonly');
		document.getElementById('targetvalue').setAttribute('readonly', 'readonly');
		document.getElementById('measurename').setAttribute('readonly', 'readonly');
		
		document.getElementById('startvalue').value = "0";
		document.getElementById('currentvalue').value = "0";
		document.getElementById('targetvalue').value = "100";
		document.getElementById('measurename').value = "%";
	}
	
}
